#!/bin/sh
# inv_dl.sh: invidious downloader
# usage: inv_dl.sh <invidious url>

# instances are separated by spaces so the for loop can parse them as separate strings
instances="yt.artemislena.eu inv.riverside.rocks vid.puffyan.us"
# the other flag is because some instances return a 403
flags="-s -A 'Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0'"

id=`curl $flags $1 | grep 'itag=18' | awk -F '"' 'NR == 1 { print $2 }'`
url=`printf "https://vid.puffyan.us%s\n" "$id"`

for i in $instances
do
	printf "https://%s%s\n" "$i" "$id"
done

case $# in
0)	echo "no url given, exiting"; exit 2 ;;
3)	exit 2 ;;
*)
	wget -U 'Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0' "$url" -O $2
esac
